package com.pactera.madp.cp.controller.oms.channel;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.common.log.annotation.SysLog;
import com.pactera.madp.common.security.annotation.Inner;
import com.pactera.madp.cp.api.dto.channel.*;
import com.pactera.madp.cp.api.entity.CorpEntity;
import com.pactera.madp.cp.api.vo.channel.*;
import com.pactera.madp.cp.service.ChannelCodeContactStatisticsService;
import com.pactera.madp.cp.service.ICorpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;

/**
 * ChannelCodeContactStatisticsController
 *
 * @author jun
 * @version 1.0
 * 2021/8/5 18:06
 **/
@RestController
@AllArgsConstructor
@RequestMapping("/channelcodecontact")
@Api(value = "channelcodecontact", tags = "渠道活码统计信息")
public class ChannelCodeContactStatisticsController {

    private ChannelCodeContactStatisticsService channelCodeContactStatisticsService;
    private ICorpService corpService;

    @GetMapping("/details")
    @ApiOperation(value = "集合数据-渠道活码基础信息")
    @ApiImplicitParam(name="channelId",value="渠道活码id",dataType = "Integer")
    public R<ChannelCodeContactDetailsVO> channelCodeDetails(ChannelCodeContrastDetailsDTO dto){
        return R.ok(channelCodeContactStatisticsService.channelCodeDetails(dto));
    }

    @Inner
    @SysLog("同步渠道活码客户统计信息")
    @PutMapping("/syn")
    @ApiOperation(value = "同步渠道活码客户统计信息")
    public void channelCodeContact(){
        List<CorpEntity> list = corpService.list();
        list.forEach(l -> {
            setChannelCodeContactStatistics(l.getCorpId());
        });
    }

    /**
     * 渠道活码统计客户信息异步中转
     * @param corpId
     */
    @Async
    void setChannelCodeContactStatistics(Integer corpId) {
        channelCodeContactStatisticsService.saveContact(corpId);
    }

    @GetMapping("/chart")
    @ApiOperation(value = "集合数据-渠道活码图表数据折线图")
    public R<List<ChannelCodeChartDataVO>> channelCodeChartDate(ChannelCodeChartDataDTO channelCodeChartDataDTO){
        return R.ok(channelCodeContactStatisticsService.getChannelCodeChartDate(channelCodeChartDataDTO));
    }

    @GetMapping("/table-date")
    @ApiOperation(value = "集合数据-按日期查看表格-分页")
    public R<Page<ChannelCodeTableDateVO>> channelCodeTableDate(Page page, ChannelCodeTableDateDTO channelCodeTableDateDTO){
        return R.ok(channelCodeContactStatisticsService.getChannelCodeTableDate(page,channelCodeTableDateDTO));
    }

    @PostMapping("/table-date-excel")
    @ApiOperation(value = "集合数据-按日期查看表格-导出Excel")
    public void channelCodeTableDateExcel(@RequestBody ChannelCodeTableDateDTO dto, HttpServletResponse response, OutputStream out){
        channelCodeContactStatisticsService.getChannelCodeTableExcel(dto, response, out);
    }

    @GetMapping("/table-date-details")
    @ApiOperation(value = "集合数据-按日期查询表格-查看详情列表")
    public R<Page<List<ChannelCodeTableDateDetailsVO>>> channelCodeTableDateDetails(Page page, ChannelCodeTableDateDetailsDTO channelCodeTableDateDetailsDTO){
        return R.ok(channelCodeContactStatisticsService.getChannelCodeTableDateDetails(page, channelCodeTableDateDetailsDTO));
    }

    @SysLog("集合数据-按日期查询表格-查看详情列表-导出Excel")
    @PostMapping("/table-date-details-excel")
    @ApiOperation(value = "集合数据-按日期查询表格-查看详情列表-导出Excel")
    public void channelCodeTableDateDetailsExcel(HttpServletResponse response, OutputStream out,@RequestBody ChannelCodeTableDateDetailsDTO channelCodeTableDateDetailsDTO){
        channelCodeContactStatisticsService.getChannelCodeTableDateDetailsExcel(response,out,channelCodeTableDateDetailsDTO);
    }

    @GetMapping("/table-employee")
    @ApiOperation(value = "集合数据-按员工查看表格-分页")
    public R<Page<List<ChannelCodeTableEmployeeVO>>> channelCodeTableEmployee(Page page, ChannelCodeTableEmployeeDTO dto){
        return R.ok(channelCodeContactStatisticsService.getChannelCodeTableDateEmployee(page, dto));
    }

    @SysLog("集合数据-按员工查看表格-导出Excel")
    @PostMapping("/table-employee-excel")
    @ApiOperation(value = "集合数据-按员工查看表格-导出Excel")
    public void channelCodeTableEmployeeExcel(HttpServletResponse response, OutputStream out,@RequestBody ChannelCodeTableEmployeeDTO dto){
        channelCodeContactStatisticsService.getChannelCodeTableEmployeeExcel(dto,response,out);
    }

    @GetMapping("/table-employee-details")
    @ApiOperation(value = "集合数据-按员工查看表格-查看详情-分页")
    public R<Page<List<ChannelCodeTableDateDetailsVO>>> channelCodeTableEmployeeDetails(ChannelCodeTableEmployeeDetailsDTO dto,Page page){
        return R.ok(channelCodeContactStatisticsService.getChannelCodeTableEmployeeDetails(page,dto));
    }

    @SysLog("集合数据-按员工查看表格-查看详情-导出Excel")
    @PostMapping("/table-employee-details-excel")
    @ApiOperation(value = "集合数据-按员工查看表格-查看详情-导出Excel")
    public void channelCodeTableEmployeeDetailsExcel(HttpServletResponse response, OutputStream out,@RequestBody ChannelCodeTableEmployeeDetailsDTO dto){
        channelCodeContactStatisticsService.getChannelCodeTableEmployeeDetailsExcel(response, out, dto);
    }

    @GetMapping("/contrast-details")
    @ApiOperation(value = "渠道活码对比数据基础信息")
    public R<List<ChannelCodeContrastDetailsVO>> channelCodeContrastDetails(ChannelCodeContrastDetailsDTO dto){
        return R.ok(channelCodeContactStatisticsService.getChannelCodeContrastDetails(dto));
    }

    @SysLog("渠道活码对比数据基础信息-导出Excel")
    @PostMapping("/contrast-details-excel")
    @ApiOperation(value = "渠道活码对比数据基础信息-导出Excel")
    public void channelCodeContrastDetailsExcel(HttpServletResponse response, OutputStream out,@RequestBody ChannelCodeContrastDetailsDTO dto){
         channelCodeContactStatisticsService.getChannelCodeContrastDetailsExcel(response, out, dto);
    }

    @GetMapping("/contrast-line-chart")
    @ApiOperation(value = "渠道活码对比数据-折线图")
    public R<List<ChannelCodeContrastLineChartVO>> channelCodeContrastLineChart(ChannelCodeContrastLineChartDTO dto){
        return R.ok(channelCodeContactStatisticsService.getChannelCodeContrastLineChart(dto));
    }

}
